package Test03;

import javax.swing.*;
import java.awt.*;
import java.util.ArrayList;

public class Sheep {
    String[] cards;

    public Sheep(){
        cards = new String[]{"兔子", "刷子", "剪刀", "南瓜", "奶瓶", "帽子", "干草", "手套", "月饼", "树",
                "树桩", "栗子", "毛线", "水桶", "火堆", "白菜", "耙子", "草", "萝卜", "蜜蜂", "酒坛", "铃铛",
                "面具", "饭团", "苹果"};
    }

    public ArrayList<JButton> get(int counts){
        ArrayList<JButton> cardList = new ArrayList<>();
        for (int i = 0;i < counts;i++){
            for (int j =0;j < cards.length;j++){
                String cardName = "res/"+cards[j]+".png";
                String cardName2 = "res/"+cards[j]+"2.png";
                JButton card = new JButton(new ImageIcon(cardName));
                card.setDisabledIcon(new ImageIcon(cardName2));
                card.setEnabled(false);
                card.setSize(59,66);
                card.setName(cardName);
                cardList.add(card);
            }
        }
        return cardList;
    }

    public void put(ArrayList<JButton> cardList,JPanel panel,int offset,
                    int row,int list,int x,int y){
        for (int i = 0;i < row * list;i++){
            JButton card = cardList.get(i + offset);
                card.setLocation(x + (i % row)*59,y+ (i / list)*66);
                panel.add(card,0);


        }
    }

    public void addAction(ArrayList<JButton> cardList,ArrayList<JButton> troughList,JPanel panel){
        for (int i = 0;i < cardList.size();i++){
            JButton card = cardList.get(i);
            card.addActionListener(e -> {
                JButton current = (JButton) e.getSource();
//                System.out.println(current.getName());


                if (troughList.size() == 7){
                    JOptionPane.showMessageDialog(panel,"卡槽满了");
                    return;
                }

                cardList.remove(current);
                intoTough(troughList,current,panel);
                pressing(cardList);

                if (troughList.size() == 0){
                    JOptionPane.showMessageDialog(panel,"你赢辣");
                }


            });
        }
    }

    public boolean press(JButton upCard,JButton downCard){
        int x = downCard.getX() - 59;
        int x1 = downCard.getX() + 59;
        int y = downCard.getY() - 66;
        int y1 = downCard.getY() + 66;

        boolean press = (upCard.getX() > x && upCard.getX() < x1) &&
                (upCard.getY() > y && upCard.getY() < y1);
        return press;
    }

    public void pressing(ArrayList<JButton> cardList){
        for (int i = 0;i < cardList.size();i++){
            JButton downCard = cardList.get(i);
            downCard.setEnabled(true);
            for (int j = i+1;j < cardList.size();j++){
                JButton upCard = cardList.get(j);
                if (press(upCard,downCard)){
                    downCard.setEnabled(false);
                }

            }
        }
    }

    public void intoTough(ArrayList<JButton> troughList, JButton trough, JPanel panel){
        int index = 0;
//        if (index +2 > troughList.size()){
//            return;
//        }
        for (;index < troughList.size();index++){
            JButton card = troughList.get(index);
            if (trough.getName().equals(card.getName())){
                break;
            }
        }
        troughList.add(index,trough);
        if (index + 2 < troughList.size()){
            JButton nextNextCard = troughList.get(index+2);
            if (trough.getName().equals(nextNextCard.getName())){
                JButton a1 = troughList.remove(index);
                JButton a2 = troughList.remove(index);
                JButton a3 = troughList.remove(index);
                panel.remove(a1);
                panel.remove(a2);
                panel.remove(a3);
                panel.updateUI();
            }
        }





        for (int i = 0; i < troughList.size(); i++) {
            JButton cardd = troughList.get(i);
            cardd.setLocation(20 + i * 59, 640);
        }

    }
}
